package org.kabeja.dxf.entity;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.dxf.helpers.DXFTextParser;
import org.kabeja.dxf.helpers.Point;
import org.kabeja.dxf.helpers.TextDocument;

/**
 * @author shutao
 * @since 2022/4/24 17:24
 */
public class DXFText extends DXFEntity {
    public static final double DEFAULT_FONT_SIZE = 8.0D;
    public static final int VALIGN_BASELINE = 0;
    public static final int VALIGN_BOTTOM = 1;
    public static final int VALIGN_CENTER = 2;
    public static final int VALIGN_TOP = 3;
    public static final int ALIGN_LEFT = 0;
    public static final int ALIGN_CENTER = 1;
    public static final int ALIGN_RIGHT = 2;
    public static final int ALIGN_ALIGNED = 3;
    public static final int ALIGN_MIDDLE = 4;
    public static final int ALIGN_FIT = 5;
    protected double rotation = 0.0D;
    protected double height = 0.0D;
    protected double scale_x = 1.0D;
    protected double oblique_angle = 0.0D;
    protected double align_x = 0.0D;
    protected double align_y = 0.0D;
    protected double align_z = 0.0D;
    protected int align = 0;
    protected int valign = 0;
    protected String text = "";
    protected String textStyle = "";
    protected Point p = new Point();
    protected Point align_p1 = new Point();
    protected Point align_p2;
    protected boolean upsideDown = false;
    protected boolean backward = false;
    protected boolean alignmentPointSet = false;
    protected boolean top = false;
    protected boolean bottom = false;
    protected boolean vertical_center = false;
    protected TextDocument textDoc = new TextDocument();

    public DXFText() {
        this.setLineType(this.getType());
    }

    @Override
    public void setDXFDocument(DXFDocument var1) {
        super.setDXFDocument(var1);
    }

    @Override
    public Bounds getBounds() {
        Bounds var1 = new Bounds();
        double var2 = (double)this.getTextDocument().getText().length();
        if (var2 > 0.0D) {
            Point var4 = this.calculateAlignmentPoint();
            var1.addToBounds(var4);
            double var5 = this.getHeight();
            double var7 = var2 * var5 * 0.6D;
            if (this.isBackward()) {
                var7 = -1.0D * var7;
            }

            switch(this.align) {
                case 0:
                    var1.addToBounds(var4.getX() + var7, var4.getY(), var4.getZ());
                    break;
                case 1:
                    var1.addToBounds(var4.getX() + var7 / 2.0D, var4.getY(), var4.getZ());
                    var1.addToBounds(var4.getX() - var7 / 2.0D, var4.getY(), var4.getZ());
                    break;
                case 2:
                    var1.addToBounds(var4.getX() - var7, var4.getY(), var4.getZ());
                    break;
                case 3:
                    var1.addToBounds(var4.getX() - var7, var4.getY(), var4.getZ());
                    break;
                case 4:
                    var1.addToBounds(var4.getX() + var7 / 2.0D, var4.getY(), var4.getZ());
                    var1.addToBounds(var4.getX() - var7 / 2.0D, var4.getY(), var4.getZ());
                    break;
                case 5:
                    var1.addToBounds(var4.getX() + var7 / 2.0D, var4.getY(), var4.getZ());
                    var1.addToBounds(var4.getX() - var7 / 2.0D, var4.getY(), var4.getZ());
            }

            switch(this.valign) {
                case 0:
                    var1.addToBounds(var4.getX(), var4.getY() + var5 * 0.75D, var4.getZ());
                    break;
                case 1:
                    var1.addToBounds(var4.getX(), var4.getY() + var5, var4.getZ());
                    break;
                case 2:
                    var1.addToBounds(var4.getX(), var4.getY() + var5 * 0.5D, var4.getZ());
                    var1.addToBounds(var4.getX(), var4.getY() - var5 * 0.5D, var4.getZ());
                    break;
                case 3:
                    var1.addToBounds(var4.getX(), var4.getY() - var5, var4.getZ());
            }
        } else {
            var1.setValid(false);
        }

        return var1;
    }

    public int getAlign() {
        return this.align;
    }

    public void setAlign(int var1) {
        this.align = var1;
    }

    public double getAlignX() {
        return this.align_p1.getX();
    }

    public void setAlignX(double var1) {
        this.align_p1.setX(var1);
    }

    public double getAlignY() {
        return this.align_p1.getY();
    }

    public void setAlignY(double var1) {
        this.align_p1.setY(var1);
    }

    public double getAlignZ() {
        return this.align_p1.getZ();
    }

    public void setAlignZ(double var1) {
        this.align_p1.setZ(var1);
    }

    public double getHeight() {
        if (this.height != 0.0D) {
            return this.height;
        } else {
            return this.doc.getDXFStyle(this.textStyle) != null ? this.doc.getDXFStyle(this.textStyle).getTextHeight() : 0.0D;
        }
    }

    public void setHeight(double var1) {
        this.height = var1;
    }

    public double getObliqueAngle() {
        return this.oblique_angle;
    }

    public void setObliqueAngle(double var1) {
        this.oblique_angle = var1;
    }

    public double getRotation() {
        return this.rotation;
    }

    public void setRotation(double var1) {
        this.rotation = var1;
    }

    public double getScaleX() {
        return this.scale_x;
    }

    public void setScaleX(double var1) {
        this.scale_x = var1;
    }

    public String getText() {
        return this.text;
    }

    public void setText(String var1) {
        this.text = var1;
        this.textDoc = DXFTextParser.parseDXFText(this);
    }

    public String getTextStyle() {
        return this.textStyle;
    }

    public void setTextStyle(String var1) {
        this.textStyle = var1;
    }

    public int getValign() {
        return this.valign;
    }

    public void setValign(int var1) {
        this.valign = var1;
    }

    public void setX(double var1) {
        this.p.setX(var1);
    }

    public void setY(double var1) {
        this.p.setY(var1);
    }

    public void setZ(double var1) {
        this.p.setZ(var1);
    }

    public boolean isBackward() {
        return this.backward;
    }

    public void setBackward(boolean var1) {
        this.backward = var1;
    }

    public boolean isUpsideDown() {
        return this.upsideDown;
    }

    public void setUpsideDown(boolean var1) {
        this.upsideDown = var1;
    }

    @Override
    public String getType() {
        return DXFConstants.ENTITY_TYPE_TEXT;
    }

    public boolean isAlignmentPointSet() {
        return this.alignmentPointSet;
    }

    public void setAlignmentPoint(boolean var1) {
        this.alignmentPointSet = var1;
    }

    public TextDocument getTextDocument() {
        return this.textDoc;
    }

    public Point getInsertPoint() {
        return this.p;
    }

    public Point getAlignmentPoint() {
        return this.align_p1;
    }

    public Point calculateAlignmentPoint() {
        Point var1 = new Point(this.p.getX(), this.p.getY(), this.p.getZ());
        if (!this.isUpsideDown()) {
            switch(this.align) {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    if (this.alignmentPointSet) {
                        var1.setX(this.align_p1.getX());
                    }
                    break;
            }

            if (this.alignmentPointSet) {
                var1.setY(this.align_p1.getY());
            }
        }

        return var1;
    }

    @Override
    public boolean isOmitLineType() {
        return true;
    }

    @Override
    public double getLength() {
        return 0.0D;
    }
}
